﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using WooCoo.DataPortal.WebHost.Route;
using WooCooTest.Data;
using WooCooTest.Model;

namespace WebApiHost
{
    public class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            //config.MapHttpAttributeRoutes();

            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional }
            //);
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<UserInfo>("UserInfo");
            builder.EntitySet<UserInfoPOCO>("UserInfoPOCO");
            config.Routes.MapDataPotalRoute("ODataPortal", "ODataPortal", builder.GetEdmModel());

            WooCoo.RuntimeEnvironment.DefaultEntityRepositoryType = typeof(WooCoo.Data.EF.DefaultEntityRepository<>);

            WooCoo.RuntimeEnvironment.Modules.Add("WooCooTest.Model", new ModuleInfo());
            WooCooTestContainer c = new WooCooTestContainer();

            //WooCoo.RuntimeEnvironment.DbContextType = typeof(WooCooTest.Data.WooCooTestContainer);
            //WooCoo.RuntimeEnvironment.DefaultEntityRepositoryType = typeof(WooCoo.Data.EF.DefaultEntityRepository<>);
        }
        /// <summary>
        /// 当前模块信息
        /// </summary>
        public class ModuleInfo : WooCoo.IModule
        {
            public string ModuleName
            {
                get { return "WebApiHost"; }
            }

            private Type _DbContextType;
            public Type DbContextType
            {
                get
                {
                    if (_DbContextType == null)
                        _DbContextType = typeof(WooCooTest.Data.WooCooTestContainer);
                    return _DbContextType;
                }                
            }
            private Type _DefaultEntityRepositoryType;
            public Type DefaultEntityRepositoryType
            {
                get
                {
                    if (_DefaultEntityRepositoryType == null)
                        _DefaultEntityRepositoryType = typeof(WooCoo.Data.EF.DefaultEntityRepository<>);
                    return _DefaultEntityRepositoryType;
                }
            }
        }
    }
}